package com.papa91.arc;

import android.content.Context;
import android.graphics.Bitmap;

import com.papa91.arcapp.AppConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class Emulator
{

	//	static
	//	{
	//		System.loadLibrary("fbawrapper");
	//	}

	public static int CONTROL_TYPE_STATUS = 1;
	public static int CONTROL_CMD_RESUME = 1;
	public static int CONTROL_CMD_RESET = 2;
	public static int CONTROL_CMD_STOP = 3;

	public static native int soInit(int lang, String libPath, String romName, String biosPath);

	public static native int init(int multiThread);

	public static native int deinit();

	public static native int loadRom(String romPath);

	public static native int unloadRom();

	public static native int loadState(String statePath);

	public static native int saveState(String statePath);

	public static native int setStateBuff(byte[] buff, int len);

	public static native byte[] getStateBuff();

	public static native int coreLoop(int draw);

	public static native int setControl(int type, int cmd);

	public static native int drawBmp(Bitmap bmp);

	public static native int setGLQuality(int quality);

	public static native int drawGL();

	public static native int setKeyState(int player, int value);

	public static native int setSoundEnable(int enable);

	public static native int getVideoWidth();

	public static native int getVideoHeight();

	public static native int getRomInfo(String name, int type);

	public static native String getDrvInfo(int type, int index);

	public static native String getRomText(String name, int type, int index);

	public static native int getCheatOptionCount(int index);

	public static native int EmulatorWriteRAM8(int address, int value);

	public static native byte EmulatorReadRAM8(int address);

	public static native int EmulatorWriteRAM16(int address, int value);

	public static native short EmulatorReadRAM16(int address);

	public static native String getCheatOptionName(int index, int option);

	public static native int cheatEnable(int index, int option);

	public static native int ipsInit(int lang);

	public static native int addIpsPatch(String path);

	public static native int enableIpsPatch(int enable);

	public static native String getIpsPatchDesc(String path);

	public static native String signData(Context ctx);

	@SuppressWarnings("rawtypes")
	public static class SortComparator implements Comparator
	{

		@Override
		public int compare(Object object1, Object object2)
		{
			// TODO Auto-generated method stub
			String str1 = (String) object1;
			String str2 = (String) object2;
			return str1.compareTo(str2);
		}
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<String> getIpsPaths(String romName)
	{
		final String ipsPath = AppConfig.IPS_PATH + romName;
		File file = new File(ipsPath);
		File[] files = file.listFiles();
		ArrayList<String> pathList = new ArrayList<String>();
		if (files != null)
		{
			for (File f : files)
			{
				if (f.isFile())
				{
					if (f.getPath().endsWith(".dat"))
					{
						pathList.add(f.getPath());
					}
				}
			}
		}

		Comparator comp = new SortComparator();
		Collections.sort(pathList, comp);
		return pathList;
	}

}
